package com.huobao.yunying.activitys_log_reg;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.huobao.yunying.activitys.Act_Main;
import com.huobao.yunying.activitys_hehuoren.Act_Hehuoren;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.PermissionsChecker;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.yunying.utils.UserUtils;
import com.huobao.yunying.views.ClearEditText;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ExitUtil;
import com.xrz.sxm.base.utils.ToolLoading;

import org.xutils.view.annotation.ViewInject;

import java.util.Map;

public class Act_Login extends HuobaoBaseActivity implements OnClickListener {

	@ViewInject(R.id.tv_reg)
	private TextView tv_reg;
	@ViewInject(R.id.tv_findpwd)
	private TextView tv_findpwd;
	// 用户名
	@ViewInject(R.id.edt_username)
	private ClearEditText edt_username;
	// 密码
	@ViewInject(R.id.edt_pwd)
	private ClearEditText edt_pwd;
//	// 10天免登录
//	@ViewInject(R.id.checkbox)
//	private CheckBox checkbox;
	// qq登录
//	@ViewInject(R.id.tv_qq_login)
//	private TextView tv_qq_login;
//	// 微博登录
//	@ViewInject(R.id.tv_weibo_login)
//	private TextView tv_weibo_login;


	private boolean isQuick = false;

	@Override
	protected int getContentView() {
		return R.layout.layout_login;
	}

	@Override
	protected String getTitleText() {
		return "登录";
	}

	@Override protected void onResume() {
		super.onResume();


	}


	@Override
	protected void init() {
		mAbTitleBar.setVisibility(View.GONE);
		tv_reg.setOnClickListener(this);
		tv_findpwd.setOnClickListener(this);
//		tv_qq_login.setOnClickListener(this);
//		tv_weibo_login.setOnClickListener(this);
		edt_username.setText(SessionUtils.getUserName(context));
		edt_pwd.setText(SessionUtils.getPwd(context));
		isQuick = getIntent().getBooleanExtra("flag",false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_reg:
			Intent intent = new Intent(this, Act_Reg.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.tv_findpwd:
			Intent in = new Intent(this, Act_FindPwd.class);
			startActivity(in);
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if(arg0 == 1 && arg1 == RESULT_OK){
			finish();
		}
		super.onActivityResult(arg0, arg1, arg2);
	}

	public void onCloseClick(View v){
		finish();
	}

	private void loginHuanxin(String username, String password){
		ToolLoading.ShowLoading(this);
		EMChatManager.getInstance().login(username,password,new EMCallBack() {//回调
			@Override
			public void onSuccess() {
				runOnUiThread(new Runnable() {
					public void run() {
						EMGroupManager.getInstance().loadAllGroups();
						EMChatManager.getInstance().loadAllConversations();
					}
				});
				handler.sendEmptyMessage(1);
			}

			@Override
			public void onProgress(int progress, String status) {

			}

			@Override
			public void onError(int code, String message) {
				handler.sendEmptyMessage(2);
			}
		});
	}

	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			ToolLoading.closeLoading(context);
			switch(msg.what){
				case 1:
					ExitUtil.getInstant().exitApp(context);
					switch(user_role){
						case 1:
							Intent intent2 = new Intent(context, Act_Main.class);
							intent2.putExtra("role",0);
							startActivity(intent2);
							break;
						case 2:
							Intent intent = new Intent(context, Act_Main.class);
							intent.putExtra("role",2);
							startActivity(intent);
							break;
						case 3:
							Intent intent1 = new Intent(context, Act_Hehuoren.class);
							startActivity(intent1);
							break;
					}
//				EventBus.getDefault().post(user_role, "LoginSuccess");
					finish();
					break;
				case 2:
					Toast.makeText(context, "登录失败，请稍候再试！", Toast.LENGTH_SHORT).show();
					break;
			}
		}
	};

	private int user_role;
	/**
	 * 登录按钮点击
	 * 
	 * @param view
	 */
	public void onLoginClick(View view) {
		final String username = edt_username.getText().toString();
		final String pwd = edt_pwd.getText().toString();
		if (TextUtils.isEmpty(username) || TextUtils.isEmpty(pwd)) {
			Toast.makeText(context, "用户名和密码不能为空！", Toast.LENGTH_SHORT).show();
			return;
		}
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY,"login","user_name",username,"password",pwd);
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				int user_id = jsonObject.getIntValue("user_id");
				user_role = jsonObject.getIntValue("user_role");
				UserUtils.save(context, user_id, user_role);
				SessionUtils.login(context, true);
				SessionUtils.saveUser(context, username, pwd,user_role);
				//setResult(RESULT_OK);
				if(isQuick){
					Toast.makeText(context, "登录成功！", Toast.LENGTH_LONG).show();
					finish();
					return;
				}
				handler.sendEmptyMessage(1);
			}
			
			@Override
			public void onHttpError(String error) {
			}
		});
	}
}
